package com.itheima.manager;

import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserFreeze;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserFreezeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UserFreezeManager
 * @date 2022/11/10 23:04
 */
@Component
public class UserFreezeManager {

    @DubboReference
    UserFreezeService userFreezeService;

    @DubboReference
    UserInfoService userInfoService;

    // 保存冻结信息
    public ResponseEntity freezeSave(UserFreeze userFreeze) {
        // 设置冻结信息创建时间
        userFreeze.setCreated(DateUtil.formatDateTime(new Date()));
        userFreeze.setUpdated(DateUtil.formatDateTime(new Date()));

        // 调用service进行保存,冻结信息
        String messge = userFreezeService.save(userFreeze);

        // 调用service去修改userInfo的状态
        UserInfo userInfo = userInfoService.findById(Long.valueOf(userFreeze.getUserId()));
        userInfo.setUserStatus("2");
        userInfoService.update(userInfo);

        return ResponseEntity.ok(messge);
    }

    // 用户解冻操作
    public ResponseEntity unfreeze(Long userId, String frozenRemarks) {
        UserFreeze userFreeze = userFreezeService.findById(userId);
        userFreeze.setFrozenRemarks(frozenRemarks);
        userFreeze.setUpdated(DateUtil.formatDateTime(new Date(System.currentTimeMillis())));
        String message = userFreezeService.unfreezes(userFreeze);
        if (message.equals("操作成功")){
            // 查询userInfo去修改状态
            UserInfo userInfo = userInfoService.findById(Long.valueOf(userId));
            userInfo.setUserStatus("1");

            // 调用service去更新userInfo
            userInfoService.update(userInfo);
        }

        return ResponseEntity.ok(message);
    }
}
